<?php

namespace sil18\VitrineBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use sil18\VitrineBundle\Entity\Panier;
use sil18\VitrineBundle\Entity\Commande;
use sil18\VitrineBundle\Entity\LigneCommande;

class PanierController extends Controller
{
    
    public function indexAction()
    {
        return $this->render('sil18VitrineBundle:Default:contenuPanier.html.twig',
                array('titre' => 'Mon panier'));
    }
    
    public function contenuPanierAction()
    {
        $panierArticles = array();
        $session = $this->getRequest()->getSession();        
        if (!$session->has('panier')) {
            $panier = new Panier();
            $session->set('panier', $panier);
            $prixTotal = 0;
        } else {
            $panier = $session->get('panier');            
            foreach ($panier->getContenu() as $key => $value) {
                $article = $this->getDoctrine()->getManager()
                    ->getRepository('sil18VitrineBundle:Article')
                    ->find($key);  
                $panierArticles[$key] = $article;
            }      
            $prixTotal = $this->getPrixTotalPanier($panier);
        }         
        return $this->render('sil18VitrineBundle:Default:contenuPanier.html.twig',
                array('titre' => 'Mon panier',
                      'panier' => $panier,
                      'panierArticles' => $panierArticles,
                      'prixTotal' => $prixTotal
                    ));
    }
    
    public function ajoutArticleAction($idArticle, $qte=1)
    {
        $session = $this->getRequest()->getSession(); 
        
        if ($session->has('panier')) {
            $panier = $session->get('panier');
            $panier->ajoutArticle($idArticle, $qte);
        } else {
            $panier = new Panier();
            $panier->ajoutArticle($idArticle, $qte);
        }        
        $session->set('panier', $panier);
        
        return $this->redirect($this->generateUrl('contenuPanier'));
    }
    
    public function supprimeArticleAction($idArticle, $qte=1)
    {
        $session = $this->getRequest()->getSession(); 
        $panier = $session->get('panier');
        $panier->supprimeArticle($idArticle, $qte);
        $session->set('panier', $panier);
        
        return $this->redirect($this->generateUrl('contenuPanier'));
    }
    
    public function viderPanierAction()
    {
        $session = $this->getRequest()->getSession();
        if ($session->has('panier')) {
            $panier = $session->get('panier');
            $panier->viderPanier();
            $session->clear();
        }   
        
        return $this->redirect($this->generateUrl('contenuPanier'));
    }
    
    public function validationPanierAction()
    {
        $listeArticles = array();
                
        //Récupération du panier et de l'id du client stockés en session
        $session = $this->getRequest()->getSession();
        $panier = $session->get('panier');
        $idClient = $session->get('idClient');

        $client = $this->getDoctrine()->getManager()
                            ->getRepository('sil18VitrineBundle:Client')
                            ->find($idClient); 
        
        $commande = new Commande();
        $commande->setDate(new \DateTime('now'));
        $commande->setEtat(1);
        $commande->setClients($client);
        
        $em = $this->getDoctrine()->getManager();
        $em->persist($commande);
        $em->flush();  
        
        foreach ($panier->getContenu() as $key => $value) {
            $article = $this->getDoctrine()->getManager()
                            ->getRepository('sil18VitrineBundle:Article')
                            ->find($key);  
            
            $ligneCommande = new LigneCommande();
            $ligneCommande->setCommande($commande);
            $ligneCommande->setQuantite($value);
            $ligneCommande->setPrix($article->getPrix());
            $ligneCommande->setArticle($article);
            $em->persist($ligneCommande);
            $em->flush();
        }
        
        $panier->viderPanier();
        $session->set('panier', $panier);
        
        return $this->render('sil18VitrineBundle:Default:validationPanier.html.twig',
                array('titre' => 'Commande validée',
                      'idCommande' => $commande->getId()
        ));
        
    }
    
    public function getPrixTotalPanier($panier)
    { 
        $prixTotal = 0;
        foreach ($panier->getContenu() as $key => $value) {
            $article = $this->getDoctrine()->getManager()
                            ->getRepository('sil18VitrineBundle:Article')
                            ->find($key);  
            $prixTotal = $prixTotal + $article->getPrix() * $value;
        }  
        return $prixTotal;
    }
}
